package com.food.restaurantpro.fragments;

import java.util.ArrayList;
import java.util.Calendar;

import com.food.restaurantpro.Config;
import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.adapters.PromotionGalleryAdapter;
import com.food.restaurantpro.database.tables.PromotionTable;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.Promotion;
import com.food.restaurantpro.utils.AlertDialogInterface;
import com.food.restaurantpro.utils.AlertDialogManager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SaleFragment extends Fragment implements OnClickListener {
	
	private TextView tvHeader;
	private Button btnPrev;
	private Button btnNext;
	private Button btnStatus;
	private TextView tvStatus;
	private Button btnMenu;
	private TextView tvMenu; /*the same btnMenu action*/
	private Gallery mGalleryPromotion;
	private ArrayList<Promotion> mPromotionArrayList;
	private PromotionGalleryAdapter mAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_sale, container, false);
		
		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		if(tvHeader != null) {
			tvHeader.setText(R.string.tvPromotion_Title);
		}
		mGalleryPromotion = (Gallery) view.findViewById(R.id.gl_promotion);
		btnPrev = (Button) view.findViewById(R.id.gl_btnPrev);
		btnNext = (Button) view.findViewById(R.id.gl_btnNext);
		btnStatus = (Button) view.findViewById(R.id.gl_btnStatus);
		
		btnMenu = (Button) view.findViewById(R.id.gl_btnMenu);
		tvMenu = (TextView) view.findViewById(R.id.gl_tvMenu);
		
		tvStatus = (TextView) view.findViewById(R.id.gl_tvStatus);
		
		//set restaurant's status for textview status
		Calendar cal = Calendar.getInstance(); 
		int hourofday = cal.get(Calendar.HOUR_OF_DAY);
		if ( Config.MIN_HOUR_OPEN <= hourofday && hourofday < Config.MAX_HOUR_OPEN) {
			tvStatus.setText(getString(R.string.restaurent_open_now));
		}else {
			tvStatus.setText(getString(R.string.restaurent_closed));
		}
		
		//event click prev button
		btnPrev.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mGalleryPromotion.getSelectedItemPosition() > 0) {
					mGalleryPromotion.setSelection(mGalleryPromotion.getSelectedItemPosition() - 1);
				}
				
			}
		});
		
		//event click next button
		btnNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mGalleryPromotion.getSelectedItemPosition() < (mAdapter.getCount()-1)) {
					mGalleryPromotion.setSelection(mGalleryPromotion.getSelectedItemPosition() + 1);
				}
			}
		});
		
		//event click status button
		btnStatus.setOnClickListener(this);
		tvStatus.setOnClickListener(this);
		
		//event click menu button
		btnMenu.setOnClickListener(this);
		tvMenu.setOnClickListener(this);
		
		
		mGalleryPromotion.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				
				if(position > 0 && position < mAdapter.getCount() - 1) {
					btnNext.setVisibility(View.VISIBLE);
					btnPrev.setVisibility(View.VISIBLE);
				} else if(position == 0) {
					btnPrev.setVisibility(View.GONE);
					if(mAdapter.getCount() > 1) {
						btnNext.setVisibility(View.VISIBLE);
					} else {
						btnNext.setVisibility(View.GONE);
					}
				} else if(position == mAdapter.getCount() - 1) {
					btnNext.setVisibility(View.GONE);
					if(mAdapter.getCount() > 1) {
						btnPrev.setVisibility(View.VISIBLE);
					} else {
						btnPrev.setVisibility(View.GONE);
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		mGalleryPromotion.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Promotion promotion = (Promotion) mGalleryPromotion.getItemAtPosition(position);
				((MainActivity) getActivity()).showPromotionDetailFragment(promotion);
			}
		});
		
		updateGallery();
		return view;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.gl_btnMenu:
		case R.id.gl_tvMenu:
			((MainActivity)getActivity()).showMenuFragment();
			break;
		case R.id.gl_btnStatus:
		case R.id.gl_tvStatus:
			break;
		default:
			break;
		}
	}
	
	private void updateGallery() {
		if(mPromotionArrayList == null) {
			mPromotionArrayList = PromotionTable.getAllPromotions();
		}
		if (mPromotionArrayList.size() > 0) {
			mAdapter = new PromotionGalleryAdapter(getActivity(), mPromotionArrayList);
			mGalleryPromotion.setAdapter(mAdapter);
			if (mAdapter.getCount() > 2) {
				mGalleryPromotion.setSelection(1);
				btnNext.setVisibility(View.VISIBLE);
				btnPrev.setVisibility(View.VISIBLE);
			} else if(mAdapter.getCount() == 2) {
				mGalleryPromotion.setSelection(0);
				btnNext.setVisibility(View.VISIBLE);
				btnPrev.setVisibility(View.GONE);
			} else {
				mGalleryPromotion.setSelection(0);
				btnNext.setVisibility(View.GONE);
				btnPrev.setVisibility(View.GONE);
			}
		} else {
			
			String messageError = "";
			if(ManagingNetwork.isNetworkAvailable()) {
				messageError = RestaurantApp.CURRENT_CONTEXT
						.getString(R.string.SERVER_ERROR);
			} else {
				messageError = RestaurantApp.CURRENT_CONTEXT
						.getString(R.string.NETWORK_ERROR);
			}
			AlertDialogManager
					.showAlertDialog(getActivity(), new AlertDialogInterface() {

						@Override
						public void onChooseYesCallback() {
							// TODO Auto-generated method stub
						}

						@Override
						public void onChooseNoCallback() {
							// TODO Auto-generated method stub

						}
					}, false, true, RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.title_internet_error),
							messageError,
							RestaurantApp.CURRENT_CONTEXT
									.getString(R.string.ok), "", false);
		}
	}
	
	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		mGalleryPromotion.setAdapter(null);
		if(mAdapter != null) {
			mAdapter.clearMemoryCache();
		}
		if(mPromotionArrayList != null) {
			mPromotionArrayList.clear();
			mPromotionArrayList = null;
		}
		Runtime.getRuntime().gc();
	}
}
